raise DistutilsGetoptError, "invalid long option '%s': must be a string of length >= 2" % long
if not short is None and type(short) is StringType and len(short) == 1:
raise DistutilsGetoptError, "invalid short option '%s': must a single character or None" % short
self.repeat[long] = repeat
self.long_opts.append(long)
if long[-1] == '=':
if short:
short = short + ':'
long = long[0:-1]
self.takes_arg[long] = 1
else:
alias_to = self.negative_alias.get(long)
if alias_to is not None:
if self.takes_arg[alias_to]:
raise DistutilsGetoptError, "invalid negative alias '%s': aliased option '%s' takes a value" % (long, alias_to)
self.long_opts[-1] = long
self.takes_arg[long] = 0
else:
self.takes_arg[long] = 0
alias_to = self.alias.get(long)
if alias_to is not None:
if self.takes_arg[long] != self.takes_arg[alias_to]:
raise DistutilsGetoptError, "invalid alias '%s': inconsistent with aliased option '%s' (one of them takes a value, the other doesn't" % (long, alias_to)
if not longopt_re.match(long):
raise DistutilsGetoptError, ("invalid long option name '%s' " + '(must be letters, numbers, hyphens only') % long
self.attr_name[long] = self.get_attr_name(long)
if short:
self.short_opts.append(short)
self.short2long[short[0]] = long
continue
def getopt(self, args = None, object = None):
"""Parse command-line options in args. Store as attributes on object.
If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
'object' is None or not supplied, creates a new OptionDummy
object, stores option values there, and returns a tuple (args,
object). If 'object' is supplied, it is modified in place and
'getopt()' just returns 'args'; in both cases, the returned
'args' is a modified copy of the passed-in 'args' list, which
Split 'text' into multiple lines of no more than 'width' characters
each, and return the list of strings that results.
"""
if text is None:
return []
if len(text) <= width:
return [
text]
text = string.expandtabs(text)
text = string.translate(text, WS_TRANS)
chunks = re.split('( +|-+)', text)
chunks = filter(None, chunks)
lines = []
while chunks:
cur_line = []
cur_len = 0
while chunks:
l = len(chunks[0])
if cur_len + l <= width:
cur_line.append(chunks[0])
del chunks[0]
cur_len = cur_len + l
continue
if cur_line and cur_line[-1][0] == ' ':
del cur_line[-1]
break
if chunks:
if cur_len == 0:
cur_line.append(chunks[0][0:width])
chunks[0] = chunks[0][width:]
if chunks[0][0] == ' ':
del chunks[0]
lines.append(string.join(cur_line, ''))
return lines
def translate_longopt(opt):
'''Convert a long option name to a valid Python identifier by
changing "-" to "_".
'''
return string.translate(opt, longopt_xlate)
class OptionDummy:
'''Dummy class just used as a place to hold command-line option
values as instance attributes.'''
def __init__(self, options = []):
"""Create a new OptionDummy instance. The attributes listed in
'options' will be initialized to None."""
for opt in options:
setattr(self, opt, None)
if __name__ == '__main__':
text = 'Tra-la-la, supercalifragilisticexpialidocious.\nHow *do* you spell that odd word, anyways?\n(Someone ask Mary -- she\'ll know [or she\'ll\nsay, "How should I know?"].)'